package com.java110.mall.openapi.cmd.token;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.AuthenticationFactory;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.integral.IntegralDto;
import com.java110.dto.user.UserDto;
import com.java110.intf.acct.IIntegralV1InnerServiceSMO;
import com.java110.intf.user.IUserV1InnerServiceSMO;
import com.java110.po.integral.IntegralPo;
import com.java110.po.user.UserPo;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Java110Cmd(serviceCode = "token.generatorCode")
public class GeneratorCodeCmd extends Cmd {

    public static final String PREFIX_CODE = "java110_mall_";


    @Autowired
    private IUserV1InnerServiceSMO userV1InnerServiceSMOImpl;

    @Autowired
    private IIntegralV1InnerServiceSMO integralV1InnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {

        Assert.hasKeyAndValue(reqJson,"tel","未包含手机号");
        Assert.hasKeyAndValue(reqJson,"userName","未包含用户名称");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        UserDto userDto = new UserDto();
        userDto.setTel(reqJson.getString("tel"));
        userDto.setLevelCd(UserDto.LEVEL_CD_MALL);
        List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);

        if (ListUtil.isNull(userDtos)) {
            userDtos = registerUser(reqJson);
        }


        String hcCode = PREFIX_CODE + GenerateCodeFactory.getUUID();
        CommonCache.setValue(hcCode, JSONObject.toJSONString(userDtos.get(0)), CommonCache.defaultExpireTime);


        context.setResponseEntity(ResultVo.createResponseEntity(hcCode));


    }

    private List<UserDto> registerUser(JSONObject reqJson) {

        String userPassword = reqJson.getString("passwd");
        if(StringUtil.isEmpty(userPassword)){
            userPassword = AuthenticationFactory.passwdMd5(reqJson.getString("tel"));
        }
        String address = "无";
        if(reqJson.containsKey("address")){
            address = reqJson.getString("address");
        }

        //todo 注册用户
        UserPo userPo = new UserPo();
        userPo.setAddress(address);
        userPo.setUserId(GenerateCodeFactory.getUserId());
        userPo.setLevelCd(UserDto.LEVEL_CD_MALL);
        userPo.setName(reqJson.getString("userName"));
        userPo.setTel(reqJson.getString("tel"));
        userPo.setPassword(userPassword);

        int flag = userV1InnerServiceSMOImpl.saveUser(userPo);
        if (flag < 1) {
            throw new CmdException("注册失败");
        }

        //todo 创建积分账户
        IntegralPo integralPo = new IntegralPo();
        integralPo.setIntegralName(reqJson.getString("userName"));
        integralPo.setAmount("0");
        integralPo.setObjId(userPo.getUserId());
        integralPo.setLink(reqJson.getString("tel"));
        integralPo.setIntegralId(GenerateCodeFactory.getGeneratorId("11"));
        integralPo.setObjType(IntegralDto.OBJ_TYPE_PERSON);
        integralV1InnerServiceSMOImpl.saveIntegral(integralPo);

        UserDto userDto = new UserDto();
        userDto.setTel(reqJson.getString("tel"));
        userDto.setLevelCd(UserDto.LEVEL_CD_MALL);
        List<UserDto> userDtos = userV1InnerServiceSMOImpl.queryUsers(userDto);
        return userDtos;
    }
}
